#!/usr/bin/env python
# -*- coding: utf-8 -*

import clutter
from clutter import Stage, Group, Rectangle, Label
from icon import ActionIcon
from clutter import Texture
from infobar import InfoBar
from widgets_area import WidgetsArea
from gstmanager.event import EventListener

import logging, sys

import touchwizard

class IconBar(Group):
    def __init__(self, width=800, height=120, title="Default canvas"):
        Group.__init__(self)
        self.stage = stage = Stage()

        self.max_icon_height = height
        self.icon_bar_content_size = 0
        self.icon_bar_available_size = width 
        self.icon_bar_remaining_size = self.icon_bar_available_size
        self.icon_bar_contents_nb = 0
        self.icon_bar_height = 0

        background_file = touchwizard.iconbar_background_path
        self.build_background(background_file)
        self.show()

    def build_background(self, file):
        if file is None: 
            self.back = back = Rectangle()
            back.set_size(self.icon_bar_available_size, self.max_icon_height)
            back.set_color(clutter.color_from_string("LightGrey"))
        else:
            import os
            path=os.path.join(os.path.abspath(os.path.dirname(__file__)),file)
            self.back = back = Texture(path)
        back.show()
        super(Group, self).add(back)

    def get_height(self):
        return self.back.get_height()

    def add_icon_raw(self, icon):
        icon.show()
        icon.set_position(self.icon_bar_content_size, 0)
        ratio = float(self.get_height())/icon.get_height()
        icon.set_scale(ratio, ratio)
        self.add(icon)
        self.icon_bar_content_size += icon.get_width() + 5

    def add_icon(self, **kwargs):
        icon = ActionIcon(**kwargs)
        self.add_icon_raw(icon)

    def remove_icon_by_event_name(self, event_name):
        for icon in self:
            if icon != self.back:
                if icon.launch_evt == event_name:
                    self.remove(icon)
                    self.icon_bar_content_size -= icon.get_width() + 5

if __name__ == "__main__":
    b = IconBar()
    b.add_icon(file="trash")
    b.add_icon(file="trash")
    b.add_icon(file="trash")
    b.add_icon(file="trash")
    b.add_icon(file="trash")
    b.add_icon(file="trash")
    b.show()
    s = clutter.Stage()
    s.add(b)
    s.show()
    s.show_all()
    clutter.main()
